﻿<?php
	$json=array();
	$dbuser='root';
	$dbpassword='';
	$dbdatabase='jm24';
	
	$pdo=new PDO("mysql:host=localhost;port=3307;dbname={$dbdatabase}","{$dbuser}","{$dbpassword}");
	$pdo->exec('set names utf8');
	
	$sql='select * from tb_user_info';
	$smt=$pdo->query($sql);
	$rows=$smt->fetchAll(PDO::FETCH_ASSOC);
	//print_r($rows);
	
	//数据输入
	$input=array('username'=>' ','password'=>' ');
	$input['username']=$_POST['username'];
	$input['pwd']=$_POST['pwd'];
	$input['pwd']=sha1($input['pwd']);
	$error=1;
	//判断并返回
	foreach($rows as $row){
		if($row['USERNAME']==$input['username'] && $row['PWD']==$input['pwd']){
			//echo "fghi";
			getData();
			break;
		}
	}
	
	if($error==1){
		$json=array('code'=>'1');
		echo json_encode($json);
	}
	
	function getData(){
		global $json,$pdo,$input;
		$sql="SELECT username,rightnum,totalnum,costtime,rightnum/totalnum rate
				FROM tb_user_info
				ORDER BY rate DESC,totalnum DESC,costtime ASC limit 10";
		$smt=$pdo->query($sql);
		$rows=$smt->fetchAll(PDO::FETCH_ASSOC);
		$i=1;
		//获取排行榜
		foreach($rows as $row){
			$data=array();
			$data['rank']=$i;
			$data['username']=$row['username'];
			$data['rightnum']=$row['rightnum'];
			$data['totalnum']=$row['totalnum'];
			$data['costtime']=$row['costtime'];
			$data['rate']=$row['rate'];
			$json[$i]=$data;
			$i++;
		}
		
		$json['code']=200;
		echo json_encode($json);
		$error=0;
	}
?>
 